package com.hotel.controllers;

import com.github.pagehelper.PageInfo;
import com.hotel.configs.EncryptConfig;
import com.hotel.entitys.Dept;
import com.hotel.entitys.Position;
import com.hotel.entitys.RestPOJO;
import com.hotel.entitys.Staff;
import com.hotel.services.DeptService;
import com.hotel.services.PositionService;
import com.hotel.services.StaffService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags="员工信息接口")//tags:说明信息
public class StaffController {
    @Autowired
    private StaffService staffService;
    @Autowired
    private PositionService positionService;
    @Autowired
    private DeptService deptService;
    @ApiOperation(
            value = "查询所有员工信息",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    // 获取所有员工信息
    @GetMapping("/staff")
    public RestPOJO getAllStaff(Integer currentPage, Integer pageSize, String keyword){
        RestPOJO restPOJO = new RestPOJO();
        PageInfo<Staff> pageInfo = staffService.getAllStaff(currentPage,pageSize,keyword);
        if (pageInfo!=null && pageInfo.getList().size()>0){
            restPOJO.setMessage("查询员工数据成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(pageInfo);
        }else {
            restPOJO.setMessage("查询员工数据失败");
            restPOJO.setStatus(1001);
            restPOJO.setData(pageInfo);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "增加员工信息",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    // 员工增加
    @PostMapping("/staff/add")
    public RestPOJO addStaff(@RequestBody Staff staff){
        RestPOJO restPOJO = new RestPOJO();
        staff.setStaff_password(EncryptConfig.getMd5Str("123456"));
        int count = staffService.addStaff(staff);
        if (count > 0){
            restPOJO.setMessage("增加员工成功");
            restPOJO.setStatus(1000);
        }else {
            restPOJO.setMessage("增加员工失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }
    @ApiOperation(
            value = "查询单个员工信息",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    // 获取单个员工信息
    @GetMapping("/staff/{id}")
    public RestPOJO getSingleStaff(@PathVariable("id") Integer staff_id){
        RestPOJO restPOJO = new RestPOJO();
        Staff staff = staffService.getStaffById(staff_id);
        List<Position> positionList = positionService.getAllPosition();
        List<Dept> deptList = deptService.getAllDepts();
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("staff",staff);
        map.put("positionList",positionList);
        map.put("deptList",deptList);
        restPOJO.setData(map);
        return restPOJO;
    }

    @ApiOperation(
            value = "修改单个员工信息",
            httpMethod = "PUT",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    // 修改员工信息
    @PutMapping("/staff")
    public RestPOJO updateStaff(@RequestBody Staff staff){
        RestPOJO restPOJO = new RestPOJO();
        int count = staffService.changeStaff(staff);
        if (count > 0){
            restPOJO.setMessage("修改员工成功");
            restPOJO.setStatus(1000);
        }else {
            restPOJO.setMessage("修改员工失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }
    @ApiOperation(
            value = "删除单个员工信息",
            httpMethod = "DELETE",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @DeleteMapping("/staff/{id}")
    public RestPOJO deleteSingleStaff(@PathVariable("id") Integer staff_id){
        RestPOJO restPOJO = new RestPOJO();
        int count = staffService.deleteOne(staff_id);
        if (count > 0){
            restPOJO.setMessage("删除员工成功");
            restPOJO.setStatus(1000);
        } else {
            restPOJO.setMessage("删除员工失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }
    @ApiOperation(
            value = "批量删除员工信息",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @PostMapping("/staff/delAll")
    public RestPOJO deleteMultiStaff(@RequestBody Object[] ids){
        RestPOJO restPOJO = new RestPOJO();
        int count = staffService.deleteMulti(ids);
        if (count > 0){
            restPOJO.setMessage("批量删除员工成功");
            restPOJO.setStatus(1000);
        }else {
            restPOJO.setMessage("批量删除员工失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }
    @ApiOperation(
            value = "查询维护部员工信息",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @GetMapping("/staff/fix")
    public RestPOJO selAllFix(){
        RestPOJO restPOJO = new RestPOJO();
        List<Staff> list = staffService.selAllFix();
        if (!list.isEmpty()){
            restPOJO.setMessage("查询维护部员工成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(list);
        }else {
            restPOJO.setMessage("查询维护部员工失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }

}